package bb.lanxing.activity.routebook

import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import bb.lanxing.R
import bb.lanxing.databinding.ActivityImportRouteBookBinding
import bb.lanxing.mvvm.base.BaseDBActivity
import com.bes.bessdk.service.base.BesServiceListener
import com.bes.sdk.device.HmDevice
import com.bes.sdk.message.Constants
import com.besall.allbase.bluetooth.service.Protocol.ProtocolService
import com.besall.allbase.view.activity.chipstoollevel4.protocol.ProtocolPresenter

class RouteBookImportActivity : BaseDBActivity<ActivityImportRouteBookBinding>(),
    BesServiceListener, DialogInterface.OnCancelListener {
    private var protocolService: ProtocolService? = null

    override fun getLayoutId() = R.layout.activity_import_route_book

    override fun initView(binding: ActivityImportRouteBookBinding) {
        setupActionBar(true)
        setTitle(R.string.downloaded_route_book)
    }

    override fun onCreate(bundle: Bundle?) {
        super.onCreate(bundle)
        setProtocolListener()
    }

    override fun onTotaConnectState(state: Boolean, hmDevice: HmDevice?) {
    }

    override fun onErrorMessage(msg: Int, hmDevice: HmDevice?) {
    }

    override fun onStateChangedMessage(msg: Int, msgStr: String?, hmDevice: HmDevice?) {
    }

    override fun onSuccessMessage(msg: Int, hmDevice: HmDevice?) {
        if (msg == Constants.TOTAL_ROUTE_FILES_ALL_TRANS_SUCCESS) {
            dismissLoadingDialog()
            runOnUiThread {
                Toast.makeText(
                    activity, "Sync Route success", Toast.LENGTH_SHORT
                ).show()
            }
        }
    }

    override fun onCancel(dialog: DialogInterface?) {
        protocolService?.bikewatch_send_giveup_trans_file(this)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        setProtocolListener()
    }

    private fun setProtocolListener() {
        val protocolPresenter = ProtocolPresenter.getInstance()
        protocolPresenter.setProtocolServiceListener(this)
        protocolService = protocolPresenter.protocolService
    }
}